Ext = {};
Ext.namespace = function()
{
    var a = arguments, o = null, i, j, d, rt;
    for (i = 0,ilen=a.length; i < ilen; ++i) 
    {
        d = a[i].split(".");
        rt = d[0];
        eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
        for (j = 1,jlen=d.length; j < jlen; ++j) 
        {
            o[d[j]] = o[d[j]] ||
            {};
            o = o[d[j]];
        }
    }
}

Ext.namespace("Milan");
Ext.namespace("Milan.navigator");
Milan.navigator.isFF = false;
Milan.navigator.isIE = false;
Milan.navigator.isOpera = false;
Milan.navigator.isSafari = false;
Milan.navigator.isMacOS = false;
Milan.navigator.isChrome = false;
Milan.navigator.isAndroid = false;
Milan.navigator.doDetect  = function()
{
	if (navigator.appName.indexOf("Microsoft") != -1) 
	{
		Milan.navigator.isIE = true;
		Milan.navigator.name = "Internet Explorer";
		Milan.navigator.version = navigator.userAgent.substr(navigator.userAgent.indexOf("MSIE")+5,3);
		return;
	} 
	
	if (navigator.userAgent.indexOf("Firefox") != -1) 
	{
		Milan.navigator.isFF = true;
		Milan.navigator.name = "Firefox";
		Milan.navigator.version = navigator.userAgent.substr(navigator.userAgent.indexOf("Firefox")+8);
		return;
	}
	
	if (navigator.userAgent.indexOf("Namoroka") != -1) 
	{
		Milan.navigator.isFF = true;
		Milan.navigator.name = "Firefox";
		Milan.navigator.version = navigator.userAgent.substr(navigator.userAgent.indexOf("Namoroka")+9);
		return;
	}
	
	if (navigator.userAgent.indexOf("Chrome") != -1) 
	{
		Milan.navigator.isChrome = true;
		Milan.navigator.name = "Chrome";
		Milan.navigator.version = navigator.userAgent.substr(navigator.userAgent.indexOf("Chrome")+7);
		return;
	}
	
	if (navigator.userAgent.indexOf("Macintosh") != -1) 
	{
		Milan.navigator.isMacOS = true;
		Milan.navigator.name = "MacOS";
		return;
	}
	if(navigator.userAgent.indexOf("Android")!=-1){
		Milan.navigator.isAndroid=true;
		Milan.navigator.name = "Android";
		return;
	}
	if (((navigator.userAgent.indexOf("Safari") != -1&&!Milan.navigator.isChrome) || (navigator.userAgent.indexOf("Konqueror") != -1))&&!Milan.navigator.isAndroid) 
	{
		Milan.navigator.isSafari = true;
		Milan.navigator.name = "Safari";
		Milan.navigator.version = parseFloat(navigator.userAgent.substr(navigator.userAgent.indexOf("Version") + 8, 3));
		return;
	} 

	if (navigator.userAgent.indexOf("Opera") != -1) 
	{
		Milan.navigator.isOpera = true;
		Milan.navigator.name = "Opera";
		Milan.navigator.version = parseFloat(navigator.userAgent.substr(navigator.userAgent.indexOf("Opera") + 6, 3));
		return;
	} 
};
Milan.navigator.doDetect();
/**
 * Ajax请求的基础类，可以处理普通form请求
 * 以及文件上传请求
 * 
 * config参数：
 * method	请求方式，一般为post
 * url		请求的url
 * params	请求的参数，应该为&a=?&b=?形式。第一个&没有也可以，当form参数有设置时，将form的参数将加上此参数一起提交。
 * form		请求时要提交的form,如果url参数没有设置，则form.action将成为url
 * isUpload	请求时是否按上传文件来处理，如果form参数有设置且form.enctype=="multipart/form-data"时，此参数自动为true
 * success		请求成功返回时的回调函数
 * failure		请求失败时的回调函数
 */
Milan.ajax = 
{
  request:function(config)
  {
	
		var url,method,isUpload,formId,form,params;
		var successCallback,failureCallback;
		url = config["url"];
		var formParam = "";
		if (config["form"]) 
		{
			formId = config["form"];
			form = $(formId);
			url = url||form.action;
			formParam += form.serialize();
		}
		method = config["method"]||form.method;
		params = config["params"];
		//successCallback = config["success"];
		failureCallback = function()
		{
			if (config && config['failure'])
				config["failure"].call();

		}
		successCallback = function(html,succ,request)
		{
			var result= request;			
			if (config && config['success'])
				config["success"].call(this,result);
		}
		if(method.toUpperCase()=="GET")
		{
			if (!config["noTimeStamp"])
			{
				if(url.indexOf("?")>1)
				{
					url +=  "&timestamp=" + (new Date()).getTime();
				}
				else
				{
					url +=  "?timestamp=" + (new Date()).getTime();
				}
			}
		}
		if(params)
		{
			if(typeof params=="object")
			{
				formParam += "&"+this.serilizeObject(params);
			}
			else
			{
				if (params.indexOf("&")==0)
					formParam = params;
				else	
					formParam += "&"+params;
			}
		}
		
		if(isUpload||(form&&(form.enctype&&form.enctype=="multipart/form-data")))
		{
			this.uploadFile(config);
			return ;
		}
    jQuery.ajax({
    	type:method,	
    	url:url,
    	data:formParam,
    	success:successCallback,
    	error:failureCallback,
    	dataType:config["dataType"]||'text',
    	processData:false
  	});
  },
	uploadFile : function(config)
	{
		var cnt = $("dialogDiv");
		var id = "_uploadFileIframe_";
	        var frame = document.createElement('iframe');
	        frame.id = id;
	        frame.name = id;
	        frame.className = 'x-hidden';
	        //document.body.appendChild(frame);
	        
	        cnt.appendChild(frame);
	        if(Milan.navigator.isIE){
	           document.frames[id].name = id;
	        }
	        var form = $(config["form"]);
	        form.target = id;
	        form.method = 'POST';
	        form.enctype = form.encoding = 'multipart/form-data';
	        function cb()
	        {
			var r = 
			{  
				responseText : '',
				responseXML : null
			};
			try 
			{ 
				var doc;
				if(Milan.navigator.isIE)
				{
				    doc = frame.contentWindow.document;
				}
				else 
				{
				    doc = (frame.contentDocument || window.frames[id].document);
				}
				if(doc && doc.body)
				{
				    //r.responseText = doc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
				    r.responseText = doc.documentElement.innerHTML;
				}
				if(doc && doc.XMLDocument)
				{
				    r.responseXML = doc.XMLDocument;
				}
				else 
				{
				    r.responseXML = doc;
				}
			}
			catch(e) 
			{
			
			}
			Event.stopObserving(frame, "load", cb);
			
			config["success"](r);
	            	setTimeout(function(){cnt.removeChild(frame);}, 100);
		}
	        
		Event.observe(frame, "load", cb);
	        form.submit();
	},
	serilizeObject : function(o)
	{
		var str = "";
		for(var a in o)
		{
			str += a + "=" + o[a] + "&";
		}
		str.substring(0,str.length-1);
		return str;
	}
}


Ext.namespace("Milan.string");

/**
 * 以规则判断是否是合法的字符串
 * strText:要判断的字符串
 * cusRegular:规则字符串，如："&*!@"即，代表strText中含有& * ! @时，认为是非法字符串。
 * isRegAdd:是否按追加模式，为true时，函数将自动以默认规则+给定的cusRegular进行判断，为false,则只以cusRegular判断。
 * 默认规则为: [':','*','"','\\','<','>','/','\'']
 */
Milan.string.isValidText = function(strText,cusRegular,isRegADD)
{
	var strRegular= new Array();
	if (cusRegular!=null && !cusRegular.trim()=="")
	{
		var aa =0;
		
		if (isRegADD) 
		{
			strRegular = [':','*','"','\\','<','>','/','\'']
			aa=strRegular.length;
		}
		for (var i=0,ilen=cusRegular.length;i<ilen;i++)
		{
			strRegular[i+aa]=cusRegular.substring(i,i+1);
		}
	}	
	
	for (var i=0,ilen=strRegular.length;i<ilen;i++)
	{
		if (strText.indexOf(strRegular[i])>-1)
			return false;
	}
	return true;
}
String.prototype.isValidText = function()
{
	return Milan.string.isValidText(this);
}


//判断是否是合法的域名
Milan.string.isDomainName = function(str) {
	var regs=/^(([a-z0-9A-Z_][a-z0-9A-Z-_]*\.)*)[a-z0-9A-Z_][a-z0-9A-Z-_]*(\.[a-zA-Z]{2,4}){1,2}$/gi;
	return regs.test(str)
}
String.prototype.isValidDomainName = function()
{
	return Milan.string.isDomainName(this);
}


//判断是否是合法的IP地址
Milan.string.isIPAddr = function(str) {
	var regs=/^(([01]?[\d]{1,2})|(2[0-4][\d])|(25[0-5]))(\.(([01]?[\d]{1,2})|(2[0-4][\d])|(25[0-5]))){3}$/
	return regs.test(str)
};
String.prototype.isIPAddr = function()
{
	return Milan.string.isIPAddr(this);
}

//判断是否合法的Email地址
Milan.string.isEmail = function(str) {
	return new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(str);
};
String.prototype.isEmail = function()
{
  return Milan.string.isEmail(this);
}

//判断字符串是否是正确的国内电话号码，包括国内手机
//ruleType参数，默认为1:
//1=国内号码，包括电话和手机
//2=国内电话
//3=国内手机
Milan.string.isPhone = function(strText,ruleType)
{
	var str = strText;
	var regepr ;
	if (!ruleType)
		ruleType = 1;
	switch(ruleType)
	{
		case 1:		//国内号码，包括电话和手机
			regepr = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)|(^0{0,1}15[0-9]{9}$)/ ;
			break;
		case 2:		//国内电话
			regepr = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)/ ;	
			break;
		case 3:		//国内手机
			regepr = /^0{0,1}(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/;
			break;
	}
	
	 
	if (!regepr.test(str))
	{
      	return false;
   	}
   return true;
}
String.prototype.isPhone = function(ruleType)
{
	return Milan.string.isPhone(this,ruleType);
}

//将字符串前后的后格去掉
Milan.string.trim = function(str) {
	 return str.replace(/(^[\s]*)|([\s]*$)/g,  "");
};
String.prototype.trim  =  function()
{
   return Milan.string.trim(this);
}

//判断字符串是否为空字符串，参数不存在时也认为是空，全为空格时也认为是空。
Milan.string.isEmpty = function(str) 
{
	if(!str)
		return true;
	
	if (str.trim().length == 0) 
		return true;
	
	return false;
};
String.prototype.isEmpty = function()
{
	return Milan.string.isEmpty(this);
}


//判断字符串是否为整数
//notNegative参数为true时，将判断是否为负数，为负时返回false
//notNegative默认为false,即不判断正负。
//decimal参数为最多允许带几位小数
//decimal默认为0,即不允许带小数点
Milan.string.isNumber = function(str,notNegative,decimal)
{
	if (!str) return false;
	str=str.trim();
	if (str.length == 0) 
		return false;
		
	if (!notNegative)		//非整数
		notNegative = false;
	if (!decimal)
		decimal = -1;
		
	try
	{
		if (isNaN(str)||isNaN(parseInt(str)))		//不能进行转换的肯定就不是数字了，可以直接pass
			return false;
	}
	catch(e)
	{
		return false;
	}
	
	if (decimal && str.indexOf(".")>=0)
	{
		if (decimal==0)
		{
			if (str.indexOf(".")>=0)
				return false;
		}
		else
		{
			var a = str.length-str.indexOf(".")-1;	//小数点后的位数
			if (a>decimal)
				return false;
		}
	}
	if (notNegative)
	{
		if (str.indexOf("-")>=0)
			return false;
	}
	return true;
}
String.prototype.isNumber = function(notNegative)
{
	return Milan.string.isNumber(this,notNegative);
}

//判断字符串是否是邮政编码
Milan.string.isPostCode = function(str)
{
	var regx = /^\d{6}$/;
	return regx.test(str);
}
String.prototype.isPostCode = function()
{
	return Milan.string.isPostCode(this);
}
	
//获得字符串的真正长度，汉字会被算成2位
Milan.string.realLength=function(strTemp){
	var i,sum;    
	sum=0;    
	for(i=0;i<strTemp.length;i++)    
	{    
	  if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))    
	    sum=sum+1;    
	  else   
	    sum=sum+2;    
	}    
	return sum;
};

String.prototype.realLength = function()
{
	return Milan.string.realLength(this);
}

/**
 * 将html特殊字符 " < > / \ ‘ 转意
 */
Milan.string.escapeHtmlChar = function(str)
{
	str = str.replace(/\'/gi,"&#39;");
	str = str.replace(/\"/gi,"&#34;");
	str = str.replace(/\</gi,"&#60;");
	str = str.replace(/\>/gi,"&#62;");
	str = str.replace(/\//gi,"&#47;");
	str = str.replace(/\\/gi,"&#92;");
	str = str.replace(/\?/gi,"&#63;");
	return str;
}
String.prototype.escapeHtmlChar = function()
{
	return Milan.string.escapeHtmlChar(this);
}

//给String一个新的方法，用于获取url中的paramter值
Milan.string.getParameter = function(url,parName) 
{ 
	var reg = new RegExp("(^|&)"+ parName +"=([^&]*)(&|$)"); 
	var r = url.substr(url.indexOf("?")+1).match(reg); 
	if (r!=null) return unescape(r[2]); return null; 
} 

String.prototype.getParameter = function(parName)
{
	return Milan.string.getParameter(this,parName);
}

Milan.domLoadingTimer = function(callback,delay)
{
	function tt()
	{
		try{
			callback();
			return;
		}
		catch(e)
		{
			//alert("e:" + e.description);
			setTimeout(tt,delay);
		}
	}
	tt();
}




if(!Milan.navigator.isIE)
{ //firefox innerText define
	HTMLElement.prototype.__defineGetter__("innerText",
		function()
		{
		var anyString = "";
		var childS = this.childNodes;
		for(var i=0,ilen=childS.length; i<ilen; i++) 
		{
		 	if(childS[i].nodeType==1)
			 //anyString += childS[i].tagName=="BR" ? "\n" : childS[i].innerText;
			 anyString += childS[i].innerText;
		 	else if(childS[i].nodeType==3)
			 anyString += childS[i].nodeValue;
		}
		return anyString;
		}
	);
	
	HTMLElement.prototype.__defineSetter__("innerText",
	function(sText){
	 this.textContent=sText;
	}
	);
}
jQuery.fn.outerHTML = function(s) {   
    return (s)? this.before(s).remove(): jQuery("<p>").append(this.eq(0).clone()).html();   
}